class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n), g(n); // 以i结尾的所有子数组的最大/小乘积
        int ret = f[0] = g[0] = nums[0];
        for(int i = 1; i < n; ++i)
        {
            f[i] = max(nums[i], max(nums[i] * f[i-1], nums[i] * g[i-1]));
            g[i] = min(nums[i], min(nums[i] * f[i-1], nums[i] * g[i-1]));
            ret = max(ret, f[i]);
        }
        return ret;
    }
};